// pages/my/index.js
const App = getApp();
const baseUrl = require('../../api/config').dev //这里用的是ES6的写法
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
const WXAPI = require("../../api/api");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentSelectOrderType:'2',
    myBg:`${baseUrl['baseUrl']}/files/appletstaticimg/my-bg.png`,
    bannerBg:`${baseUrl['baseUrl']}/files/appletstaticimg/my-banner.png`,
    headImg: '',
    nickname: '',
    userInfo:{}
  },
  onLoad(){
  },
  onShow() {
    // let headImg
    // let nickname
    // if (App.globalData.userInfo.headimage) {
    //   headImg = `${baseUrl['baseUrl']}/files/${App.globalData.userInfo.headimage}`
    //   nickname = App.globalData.userInfo.nickname
    // } 
    // this.setData({
    //   headImg: headImg,
    //   nickname: nickname
    // })
    this.getUserInfos()
  },
  async getUserInfos(){
    let res = await WXAPI.getUser()
    if(res.data.code == 0) {
      this.setData({
        headImg:`${baseUrl['baseUrl']}/files/${res.data.data.sHeadimgurl}`,
        nickname: res.data.data.sNickname,
        userInfo: res.data.data
      })
    }
  },
  appointRecords(){
    wx.navigateTo({
      url: '/packageA/pages/org-appoint-records/index',
    })
  },
  toOrg(){
    wx.navigateTo({
      url: '/packageA/pages/org-my-collect/index',
    })
  },
  toMessage(){
    wx.switchTab({
      url: '/pages/message/index',
    })
  },
  toCouponList(){
    wx.navigateTo({
      url: `/packageA/pages/coupon-list/index`,
    })
  },
  // async getUserInfo(){
  //   let res = await  WXAPI.getUser()
  //   if(res.data.code == 0) {
  //     console.log('res.data',res.data);
  //     let obj = {
  //       sNickname:res.data.data.sNickname,
  //       sHeadimgurl:res.data.data.sHeadimgurl
  //     }
  //     App.setUserInfo(obj)
  //     this.setData({
  //       headImg: res.data.data.sHeadimgurl,
  //       nickname: res.data.data.sNickname
  //     })
  //   }
  // },
  selectOrderType(val){
    this.setData({
      currentSelectOrderType: val.detail.code
    })
  },
  toAllOrderList(event){
    let title1 = {
      "0":"全部订单",
      "1":"待付款",
      "2":"待收货",
      "3":"待评价",
      "4":"已取消",
    }
    let title2 = {
      "0":"全部订单",
      "3":"待服务",
      "4":"待完成",
      "5":"已完成",
      "9":"已取消"
    }
    let title
    if(this.data.currentSelectOrderType == 1) {
      // 商品订单
      title = title1
    }
    if(this.data.currentSelectOrderType == 2) {
      // 服务订单
      title = title2
    }
      wx.navigateTo({
        url: `/packageA/pages/all-order/index?orderType=${this.data.currentSelectOrderType}&navigationBarTitle=${title[event.currentTarget.dataset.status]}&status=${event.currentTarget.dataset.status}`,
      })
    
  },
  // 头像监听
  headClick() {
    wx.navigateTo({
      url: `/pages/my/user-edit/index?userInfo=${JSON.stringify(this.data.userInfo)}`,
    })
  },
  collectService(){
    wx.navigateTo({
      url: '/packageA/pages/my-collect-service/index',
    })
  },
  clear(){
    Dialog.confirm({
      title: '提示',
      message: '是否确认清除缓存！',
    })
      .then(() => {
        // on confirm
        Toast.success('清除成功！');
      })
      .catch(() => {
        // on cancel
      });
  },
  tell(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel
    })
  },
  // 二级菜单监听
  tabClick(e) {
    var info = e.currentTarget.dataset.item;
    wx.showToast({
      title: info.title,
      icon: 'none'
    })
    switch (info.id) {
      case '1':
        wx.navigateTo({
          url: '菜单一需要跳转的链接路径',
        })
        break;
      case '2':
        wx.navigateTo({
          url: '菜单二需要跳转的链接路径',
        })
        break;
      case '3':
        wx.navigateTo({
          url: '菜单三需要跳转的链接路径',
        })
        break;
      default:
        wx.navigateTo({
          url: '菜单四需要跳转的链接路径',
        })
        break;
    }
  },
  toAddressManage(){
    wx.navigateTo({
      url: '/packageA/pages/address-manage/index',
    })
  },
  // 基本信息
  basicClick() {
    wx.showToast({
      title: '基本信息监听',
      icon: 'none'
    })
    wx.navigateTo({
      url: '需要跳转的链接路径',
    })
  },
  // 匿名反馈
  feedbackClick() {
    wx.showToast({
      title: '匿名反馈监听',
      icon: 'none'
    })
    wx.navigateTo({
      url: '需要跳转的链接路径',
    })
  },
  // 关于我们
  aboutClick() {
    wx.showToast({
      title: '关于我们监听',
      icon: 'none'
    })
    wx.navigateTo({
      url: '需要跳转的链接路径',
    })
  },
  // 页面滚动监听
  onPageScroll(e) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },
})